import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest {

    private   double balance;
    private Lock accountLock = new ReentrantLock();

    public synchronized void  add(double money){
         //accountLock.lock();

        accountLock.lock();

        try {
            double newBalance = balance + money;
//            try {
//                //Thread.sleep(10);   // 模拟此业务需要一段处理时间
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            }
            balance = newBalance;
        } finally {
            accountLock.unlock();
        }
    }
    private double getBalance(){
        return balance;
    }

    public static void main(String[] args){
        ThreadTest tt = new ThreadTest();
        ExecutorService service = Executors.newFixedThreadPool(100);
        for(int i=0;i<100;i++){
            service.execute(new Runnable() {
                @Override
                public void run() {
                    tt.add(1);
                }
            });


            /*try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        }
//        /** 关闭线程 */
//        service.shutdown();
//        /** 若关闭后所有任务都已完成，则返回true */
//        while(!service.isTerminated()) {}
        System.out.println("最后的值:"+tt.getBalance());
    }

}
